/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2024 All Rights Reserved.
 */
package com.alipay.demo.controller;

import com.alipay.api.domain.AppxCategoryVO;
import com.alipay.api.internal.util.StringUtils;
import com.alipay.api.response.AlipayOpenAppItemListQueryResponse;
import com.alipay.api.response.AlipayOpenAppItemQueryResponse;
import com.alipay.api.response.AlipayOpenAppItemTemplateQueryResponse;
import com.alipay.demo.service.ItemService;
import com.alipay.demo.vo.ItemCreateRequest;
import com.alipay.demo.vo.OpenResponse;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jishupei.jsp
 * @version : ItemController, v0.1 2024年05月11日 2:45 下午 jishupei.jsp Exp $
 */
@Api(tags = "普通商品模块")
@ApiSupport(order = 1)
@RestController
@RequestMapping(value = "/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    /**
     * 类目查询
     * 获取叶子节点商品类目列表
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "类目查询", notes = "获取叶子节点商品类目列表")
    @GetMapping(value = "/category/list")
    public OpenResponse<List<AppxCategoryVO>> categoryList(
            @ApiParam(value = "商品类型：1-售卖类（实物），2-租赁类（实物），3-付费充值/兑换类（虚拟）") @RequestParam String itemType,
            @ApiParam(value = "类目状态，默认AUDIT_PASSED", example = "AUDIT_PASSED") @RequestParam(required = false) String catStatus) {
        return itemService.getCategories(itemType, catStatus);
    }

    /**
     * 普通商品模板信息查询
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "普通商品模板信息查询")
    @GetMapping(value = "/template/query")
    public OpenResponse<AlipayOpenAppItemTemplateQueryResponse> queryTemplate(
            @ApiParam(value = "类目ID", required = true) @RequestParam String categoryId,
            @ApiParam(value = "商品类型：1-售卖类（实物），2-租赁类（实物），3-付费充值/兑换类（虚拟）") @RequestParam(required = false) String itemType) {
        return itemService.queryTemplate(categoryId, itemType);
    }

    /**
     * 售卖类（实物）商品创建
     */
    @ApiOperation(value = "售卖类（实物）商品创建")
    @ApiOperationSupport(order = 3)
    @PostMapping(value = "/sale/create")
    public OpenResponse<String> saleCreate(@RequestBody ItemCreateRequest request) {
        return itemService.createSaleItem(request);
    }

    /**
     * 租赁类（实物）商品创建
     */
    @ApiOperation(value = "租赁类（实物）商品创建")
    @ApiOperationSupport(order = 4)
    @PostMapping(value = "/rent/create")
    public OpenResponse<String> rentCreate(@RequestBody ItemCreateRequest request) {
        return itemService.createRentItem(request);
    }

    /**
     * 付费充值/兑换类（虚拟）商品创建
     */
    @ApiOperation(value = "付费充值/兑换类（虚拟）商品创建", notes = "电影/演出/体育赛事票务类商品")
    @ApiOperationSupport(order = 5)
    @PostMapping(value = "/virtual/create")
    public OpenResponse<String> virtualCreate(@RequestBody ItemCreateRequest request) {
        return itemService.createVirtualItem(request);
    }

    /**
     * 售卖类（实物）商品更新
     */
    @ApiOperation(value = "商品更新")
    @ApiOperationSupport(order = 6)
    @PostMapping(value = "/update")
    public OpenResponse<String> update(@RequestBody ItemCreateRequest request) {
        return itemService.updateItem(request);
    }

    /**
     * 商品详情查询
     */
    @ApiOperation(value = "商品详情查询")
    @ApiOperationSupport(order = 7)
    @GetMapping(value = "/query")
    public OpenResponse<AlipayOpenAppItemQueryResponse> query(@ApiParam(value = "支付平台侧商品ID") @RequestParam(required = false) String itemId,
                                                              @ApiParam(value = "商家侧商品ID") @RequestParam(required = false) String outItemId,
                                                              @ApiParam(value = "是否查询编辑版本，默认为0", defaultValue = "0") @RequestParam(required = false) String needEditSpu) {
        return itemService.queryItemDetail(itemId, outItemId,
                StringUtils.isNumeric(needEditSpu) ? Long.parseLong(needEditSpu) : 0);
    }

    /**
     * 分页查询商品列表
     */
    @ApiOperation(value = "分页查询商品列表")
    @ApiOperationSupport(order = 8)
    @GetMapping(value = "/list/query")
    public OpenResponse<AlipayOpenAppItemListQueryResponse> queryList(@ApiParam(value = "支付平台侧商品ID") @RequestParam(required = false) String itemId,
                                                                      @ApiParam(value = "商家侧商品ID") @RequestParam(required = false) String outItemId,
                                                                      @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false) String pageNum,
                                                                      @ApiParam(value = "单页大小", defaultValue = "10") @RequestParam(required = false) String pageSize) {
        return itemService.queryItemList(itemId, outItemId,
                StringUtils.isNumeric(pageNum) ? Long.parseLong(pageNum) : 1,
                StringUtils.isNumeric(pageSize) ? Long.parseLong(pageSize) : 10);
    }
}
